//
//  WorkflowViewController.m
//  TarPhone
//
//  Created by Christopher Anderson on 17/03/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "WorkflowViewController.h"
#import "ImageSelectionController.h"
#import "StartWorkflowCellController.h"
#import "Workflow.h"
#import "ImageChooser.h"
#import "TarPipeOAuthRequestPoster.h"
#import "CellGroupController.h"
#import "EditableCellController.h"
#import "MenuCellController.h"


@implementation WorkflowViewController

@synthesize workflow;
@synthesize requestPoster;
@synthesize titleCell;
@synthesize bodyCell;
@synthesize imageCell;
@synthesize startWorkflowCell;

- (void) setWorkflow:(Workflow*)inWorkflow {
	if (workflow != inWorkflow) {
		[inWorkflow retain];
		[self.workflow release];
		workflow = inWorkflow;
	}
}

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/


- (void)viewDidLoad {
    [super viewDidLoad];
	// Replace with propert tarpipe submitter
	self.requestPoster = [[TarPipeOAuthRequestPoster alloc] init];
	
	self.titleCell = [[EditableCellController alloc] initWithTitle:@"Title:"];
	self.bodyCell =  [[EditableCellController alloc] initWithTitle:@"Body:"];
	self.imageCell = [[ImageSelectionController alloc] initWithHostController:self];
	self.startWorkflowCell = [[StartWorkflowCellController alloc] init];
	startWorkflowCell.delegate = self;

}

- (void) constructMenuGroups {
	CellGroupController *editableGroup = [[CellGroupController alloc] initWithMenuItems:[NSArray arrayWithObjects: self.titleCell, self.bodyCell,  nil] 
																				heading:@"Details"];
	
	CellGroupController *imageGroup = [[CellGroupController alloc] initWithMenuItems:[NSArray arrayWithObject:self.imageCell] 
																			 heading:@"Image"];
	
	CellGroupController *startWorkflowGroup = [[CellGroupController alloc] initWithMenuItems:[NSArray arrayWithObject:self.startWorkflowCell] heading:nil];
	
	self.menuGroups = [NSArray arrayWithObjects:editableGroup, imageGroup, startWorkflowGroup, nil];
	
	[editableGroup release];
	[imageGroup release];
	[startWorkflowGroup release];
	
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	[self updateMenuGroups];
	[self.myTableView reloadData];
	[self setTitle:workflow.title];
}

- (void) startWorkflow {
	// Get the data from the cells and submit to TarPipe!
	NSString *title = [self.titleCell getData];
	NSString *body = [self.bodyCell getData];
	UIImage *image = [self.imageCell getData];
	
	TarpipeRequest *request = [[TarpipeRequest alloc] initWithWorkflow:self.workflow andTitle:title andBody:body andImage:image];
	NSString *result = [requestPoster postRequestToTarPipe:request];
	[request release];
	[self requestDidCompleteWithResponseString:result];
	//[[NSRunLoop currentRunLoop ] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]];

	
}

- (void) requestDidCompleteWithResponseString:(NSString *)string {
	NSLog(@"Returned from web service via delegate: %@", string);	
	[self performSelectorOnMainThread:@selector(workFlowComplete) withObject:nil waitUntilDone:YES];
}

- (void) workFlowComplete {
	[self restoreTableDelegate];
}

- (void) action {
	[self startBackgroundOperation];
}

- (NSOperation*) getOperation {
	return [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(startWorkflow) object:nil];
}


/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}



#pragma mark Table view methods


// Customize the appearance of table view cells.
/*- (UITableViewCell *)tableView:(UITableView *)atableview cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [atableview dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    
	cell.text = @"Select image";
    // Set up the cell...

    return cell;
}*/

/*
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
	ImageChooser *imageChooser = [[ImageChooser alloc] initWithViewController:self];
	[self presentModalViewController:imageChooser animated:YES];
	[imageChooser release];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/




- (void)dealloc {
    [super dealloc];
}


@end

